"""
工具函数，用于处理请求相关的操作
"""

from typing import Dict, Any, Optional
from app.utils.logger import get_request_id


def get_request_id_headers() -> Dict[str, str]:
    """
    获取包含X-Request-Id的请求头，用于调用其他服务时传递请求ID
    
    Returns:
        Dict[str, str]: 包含X-Request-Id的请求头字典
    """
    request_id = get_request_id()
    if request_id and request_id != 'N/A':
        return {"X-Request-Id": request_id}
    else:
        # 如果无法获取request_id，则返回空字典
        return {}


def add_request_id_to_headers(headers: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
    """
    将X-Request-Id添加到请求头中
    
    Args:
        headers: 现有的请求头字典
        
    Returns:
        Dict[str, Any]: 包含X-Request-Id的请求头字典
    """
    if headers is None:
        headers = {}
    
    # 获取当前的request_id
    request_id_headers = get_request_id_headers()
    
    # 合并请求头
    merged_headers = headers.copy()
    merged_headers.update(request_id_headers)
    
    return merged_headers